package com.ayanami.userservice.controller;

import com.ayanami.basecommon.exception.BusinessException;
import com.ayanami.userservice.entity.TbUser;
import com.ayanami.userservice.service.TbUserService;
import com.ayanami.userservice.vo.TbUserVO;
import com.ayanami.utilscommon.response.Result;
import com.ayanami.utilscommon.util.MD5Utils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author Ayanami
 */
@Api("用户管理")
@RestController
@RequestMapping("/userservice/tb-user")
@CrossOrigin
public class LoginController {
    @Autowired
    private TbUserService tbUserService;
    @ApiOperation(value = "",notes = "")
    @PostMapping("/Login")
    public Result Login(String username,String password){
        if(StringUtils.isNotEmpty(username)&&StringUtils.isNotEmpty(password)){
            QueryWrapper<TbUser> dd=new QueryWrapper<>();
            dd.eq("username",username);
            TbUser one1 = tbUserService.getOne(dd);
            if(one1!=null) {
                password = MD5Utils.md5Encryption(password, one1.getSalt());
            }else{
                throw new BusinessException(20001,"找不到此用户名");
            }
            QueryWrapper<TbUser> queryWrapper=new QueryWrapper<>();
            queryWrapper.eq("username",username);
            queryWrapper.eq("password",password);
            TbUser one = tbUserService.getOne(queryWrapper);
            if(one!=null) {
                return Result.ok().data("user", one).data("token",one.getUsername());
            }else{
                return Result.error().message("账号或密码错误");
            }
        }else {
            return Result.error().message("账号密码为空,I`s so fucking craze!");
        }
    }
    @ApiOperation(value = "",notes = "")
    @GetMapping("/info")
    public Result findUser(){
        //固定头像，记得换头像接口
        return Result.ok().data("name","Ayanami").data("avatar","https://Ayanamides.gitee.io/myblock/img/head.jpg");
    }
    @ApiOperation(value = "分页查询",notes = "d")
    @GetMapping("findUserAndDept")
    public Result findUserAndDept(@RequestParam(required = false,defaultValue = "1")Integer current, @RequestParam(required = false,defaultValue = "5")Integer pageSize,@RequestBody TbUserVO tbUserVO){
        Page<TbUser> page=new Page<>(current,pageSize);
        QueryWrapper<TbUser> queryWrapper=new QueryWrapper<>();
        if(StringUtils.isNotEmpty(tbUserVO.getUsername())) {
            queryWrapper.like("username", tbUserVO.getUsername());
        }
        if(tbUserVO.getDepartmentId()!=null) {
            queryWrapper.eq("department_id", tbUserVO.getDepartmentId());
        }
        if(StringUtils.isNotEmpty(tbUserVO.getEmail())) {
            queryWrapper.like("email", tbUserVO.getEmail());
        }
        if(StringUtils.isNotEmpty(tbUserVO.getNickname())) {
            queryWrapper.like("nickname", tbUserVO.getNickname());
        }
        IPage<TbUser> userAndDept = tbUserService.findUserAndDept(page, queryWrapper);
        return Result.ok().data("users",userAndDept);
    }
    @ApiOperation(value = "注销",notes = "")
    @PostMapping("/logOut")
    public Result logOut(){
        return Result.ok();
    }
}
